package com.ning.ems.controller;

import com.ning.ems.dto.*;
import com.ning.ems.entity.User;
import com.ning.ems.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "用户管理相关接口")
@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation("注册")
    @PostMapping("/signUp")
    public User add(@RequestBody @Valid UserSignUpDto info) {
        User user = new User();
        user.setName(info.getName());
        user.setPassword(info.getPassword());
        user.setMobileNumber(info.getMobileNumber());
        return userService.signUp(user);
    }

    @ApiOperation("登录")
    @PostMapping("/signIn")
    public User signIn(@RequestBody @Valid UserSignInDto info) {
        User user = new User();
        user.setMobileNumber(info.getMobileNumber());
        user.setPassword(info.getPassword());
        return userService.signIn(user);
    }

    @ApiOperation("检查密码")
    @PostMapping("/checkPass")
    public boolean signIn(@RequestBody @Valid UserCheckPassDto info) {
        return userService.checkPass(info);
    }

    @ApiOperation("检查 mobileNumber 是否重复")
    @GetMapping("/check")
    public boolean checkMobileNumber(@RequestParam(value = "mobileNumber") String mobileNumber) {
        return userService.checkMobileNumber(mobileNumber);
    }

    @ApiOperation("用户列表")
    @PostMapping("/list")
    public List<User> list(@RequestBody @Valid UserListDto info) {
        return userService.list(info);
    }

    @ApiOperation("通过 id 查询用户")
    @GetMapping(value = "/get1")
    public User get(@RequestParam(value = "id") Integer id) {
        return userService.get(id);
    }

    @ApiOperation("通过 mobileNumber 查询用户")
    @GetMapping("/get2")
    public User get(@RequestParam(value = "mobileNumber") String mobileNumber) {
        return userService.get(mobileNumber);
    }

    @ApiOperation("通过 id 删除用户")
    @DeleteMapping("/remove")
    public boolean delete(@RequestParam(value = "id") Integer id) {
        return userService.delete(id);
    }

    @ApiOperation("更新用户名")
    @PutMapping("/updateName")
    public boolean updateName(@RequestBody @Valid UserUpdateDto info) {
        return userService.updateName(info.getId(), info.getAttribute());
    }

    @ApiOperation("更新密码")
    @PutMapping("/updatePassword")
    public boolean updatePassword(@RequestBody @Valid UserUpdateDto info) {
        return userService.updatePassword(info.getId(), info.getAttribute());
    }

    @ApiOperation("更新手机号码")
    @PutMapping("/updateMobileNumber")
    public boolean updateMobileNumber(@RequestBody UserUpdateDto info) {
        return userService.updateMobileNumber(info.getId(), info.getAttribute());
    }

    @ApiOperation("更新用户类型")
    @PutMapping("/updateType")
    public boolean updateType(@RequestBody UserUpdateDto info) {
        return userService.updateType(info.getId(), Integer.valueOf(info.getAttribute()));
    }

}
